package parser

import (
	"github.com/PuerkitoBio/goquery"
	"log"
	"my_git_repos/gocn_daily_news/controllers/crawl/parser/utils"
	"my_git_repos/gocn_daily_news/controllers/crawl/types"
	"strings"
)

/*
url   h4 a href
title h4 a text
user_page p a href
user_name p a text
follow,  p span[0] text
reply,
view,
publishTime
 */

func StartParser(url string) *types.Response {
	doc, err := GetDocFromUrl(url)
	if err != nil {
		log.Printf("NewDocument(%s) err: %v\n", url, err)
		return nil
	}

	res := new(types.Response)

	doc.Find("div.mod-body > div.aw-common-list > div > div.aw-question-content").Each(func(_ int, divSelection *goquery.Selection) {
		// url title
		titleEle := divSelection.Find("h4 a").First()
		url := titleEle.AttrOr("href", "")
		title := titleEle.Text()
		//fmt.Printf("title: %s, url: %s\n", title, url)

		// user_page  user_name
		userEle := divSelection.Find("p a").First()
		userPageUrl := userEle.AttrOr("href", "")
		userName := userEle.Text()
		//fmt.Printf("user_page_url: %s, user_name: %s\n", userPageUrl, userName)

		// content
		contentEle := divSelection.Find("p span").First()
		content := contentEle.Text()
		//fmt.Printf("content: %s\n", content)
		//fmt.Printf("%#v\n", ContentConstructor(content))

		page := types.Page{
			Url:url,
			Title:title,
			UserPage:userPageUrl,
			UserName:userName,
			Content: utils.ContentConstructor(content),
		}
		res.Requests = append(res.Requests, types.NewRequest(url, DayParser))
		_ = page
		//res.Contents = append(res.Contents, page)
	})

	// 处理翻页
	doc.Find("div.mod-footer div.page-control ul.pagination li a").Each(func(_ int, aSel *goquery.Selection) {
		href := aSel.AttrOr("href", "")
		if strings.Contains(href, "gocn.vip") {
			res.Requests = append(res.Requests, types.NewRequest(href, StartParser))
		}
	})

	return res
}